home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7855 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Static Member Functions
  5. Date: Mon, 19 Feb 1996 07:17:07 -0400
  6. Organization: Psychic Enemies Network
  7. Message-ID: <fcusack-1902960717070001@mudskipper.cac.psu.edu>
  8. References: <31277E66.687F@iastate.edu>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <31277E66.687F@iastate.edu>, Steve Lee <sjlee@iastate.edu> wrote:
  12.  
  13. > Hi,
  14. > I have a couple of questions about static member functions.
  15. > 1)  I was wondering why C++ doesn't provide (or maybe it does and I
  16. don't) a way to protect a 
  17. > static member function by making it constant. 
  18.  
  19. What do you mean by "protect a static member function"? from what?
  20.  
  21. > What I had in mind was something like the usage of 
  22. > const for a member function.  I know that in this sense, it is making
  23. the implicit variable *this 
  24. > constant,
  25.  
  26. Do you mean the implicit variable this (as opposed to *this)? It also
  27. makes the data in the object pointed to by this constant.
  28.  
  29. "The type of _this_ in a _const_ member function of class _X_ is
  30.  _const X *const_." [Stroustrup, 2nd Ed, p. 148; see also p. 547]
  31.  
  32. > and that static member functions don't have an implicit *this variable
  33. since they don't 
  34.  
  35. Neither do they have an implicit this variable. :)
  36.  
  37. > act on a specific object.  I want to know why this, or something similar
  38. isn't possible.
  39. > class foo {
  40. >   static int GetCount() const;
  41.  
  42. ???
  43.  
  44. since GetCount() is a static member function, it does not have a this
  45. variable; it does not act on data from a specific instance of class foo.
  46. What would the _const_ refer to?? Here, GetCount() does not take any
  47. parameters [which BTW I prefer to declare as 'GetCount(void)'] so it
  48. cannot modify data from any instance of foo - it does not have access to
  49. an object of class foo.
  50.  
  51. It _can_ modify static variables like count, below, but count is not an
  52. instance variable, so again, the _const_ declaration has no meaning.
  53.  
  54. If you intend to pass an object of class foo to GetCount(), simply declare
  55. the parameter _const_ to avoid modification.
  56.  
  57.    static int GetCount(const foo &p1);
  58.  
  59. >   static int count;
  60. > };
  61. > 2)  In the example above, count is private.  I find it interesting
  62. though that any piece of code 
  63. > can initialize the private variable count.  Isn't this contradictory? 
  64.  
  65. Regardless of whether any piece of code can initialize/define count, since
  66. count is a private member, only member functions and friends of class foo
  67. may modify it.
  68.  
  69. > The only thing I can think 
  70. > of is that the linker only allows it to be initialized once (obviously),
  71. and if the implementor 
  72. > of the class initializes it, then no users will be able to without a
  73. linker error.
  74. >
  75.  
  76. Not quite. static data members _must_ be defined/initialized exactly once;
  77. not "allowed to be [defined] once" as you put it. Thus the implementor
  78. _must_ define it, not "if the implementor...".
  79.  
  80. That is, assuming you are given a library. If you are given the source
  81. code to use, then the user can of course change the definition anyway.
  82. ~Frank
  83.  -- I am Pentium of Borg.  Division is futile.  You will be approximated. --
  84.  --   If you build it, they will come --> http://www.tdx.org/~fcusack/    --
  85.  -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F  3F 64 80 65 8B 0F F9 EA --
  86.